//
//  AffectionateViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/9.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "AffectionateViewController.h"
#import "CLLockVC.h"
#import "ChatListViewController.h"
#import "UIImageView+WebCache.h"
#import "ViewController.h"
#import <RongIMKit/RongIMKit.h>
#import <AVOSCloud.h>
#import "ChatViewController.h"
#import "ChatMainViewController.h"
@interface AffectionateViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,RCIMUserInfoDataSource,UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIButton *secretImageBtn;
@property (strong, nonatomic) IBOutlet UIImageView *photoImageView;
@property(assign ,nonatomic) BOOL isFullScreen ;
@property (strong ,nonatomic) NSString * token;
@property(strong,nonatomic) NSString *urlStr;
@property(strong,nonatomic) NSString *nameStr;

@end

@implementation AffectionateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
      UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    if (savedImage ==nil ) {
        self.photoImageView.image = [UIImage imageNamed:@"wallpaper-p3.jpg"];
    }else{
    NSLog(@"%@",savedImage);
    self.photoImageView.image = savedImage;
    }
    [[RCIM sharedRCIM] setUserInfoDataSource:self];        
    
    
}
 - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 私密相册按钮
- (IBAction)secretImageClickedAction:(id)sender {
    BOOL hasPwd = [CLLockVC hasPwd];
    
    if(!hasPwd){
        [CLLockVC showSettingLockVCInVC:self successBlock:^(CLLockVC *lockVC, NSString *pwd) {
            
            NSLog(@"密码设置成功");
            [lockVC dismiss:1.0f];
            ViewController *lockViewVC = [[ViewController alloc]init];
            [self.navigationController pushViewController:lockViewVC animated:YES];

        }];

    }else {
        
        [CLLockVC showVerifyLockVCInVC:self forgetPwdBlock:^{
            NSLog(@"忘记密码");
        } successBlock:^(CLLockVC *lockVC, NSString *pwd) {
            ViewController  *lockViewVC = [[ViewController alloc]init];
            NSLog(@"密码正确");
            [lockVC dismiss:1.0f];
            [self.navigationController pushViewController:lockViewVC animated:YES];

        }];
    }

    
}
- (IBAction)timeBtnClickedAction:(id)sender {
}

- (IBAction)textBtnClickedAction:(id)sender {

    
   
    
    
}

- (IBAction)chatBtnClickedAction:(id)sender {
//    ChatListViewController  *chatList = [[ChatListViewController alloc] init];
//    UINavigationController * chatNC = [[UINavigationController alloc]initWithRootViewController:chatList];
//    [self.navigationController presentViewController:chatNC animated:YES completion:nil];
    ChatMainViewController *chatVC = [[ChatMainViewController alloc]init];
    [self presentViewController:chatVC animated:YES completion:nil];
    
}



#pragma mark -- 更换背景按钮
- (IBAction)uploadPhotoClickedAction:(id)sender {
    UIActionSheet *sheet;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
        
    }
    
    else {
        
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
        
    }
    
    sheet.tag = 255;
    
    [sheet showInView:self.view];
    
    
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        
        NSUInteger sourceType = 0;
        
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            switch (buttonIndex) {
                case 0:
                    // 取消
                    return;
                case 1:
                    // 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                    
                case 2:
                    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
            }
        }
        else {
            if (buttonIndex == 0) {
                
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
            // 跳转到相机或相册页面
 
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        imagePickerController.delegate = self;
        
        imagePickerController.allowsEditing = YES;
        
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];

    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    /* 此处info 有六个值
     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
     * UIImagePickerControllerMediaURL;       // an NSURL
     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
     */
    // 保存图片至本地，方法见下文
    [self saveImage:image withName:@"currentImage.png"];
    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    
    _isFullScreen = NO;
    [self.photoImageView setImage:savedImage];
    
    self.photoImageView.tag = 100;
    
}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}
#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    NSLog(@"%@+++++++",fullPath);
    
    [imageData writeToFile:fullPath atomically:NO];
}



- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{
    
 
    
        RCUserInfo *user = [[RCUserInfo alloc]init];
        user.userId = userId;
        user.name = userId;
        AVQuery *query = [AVQuery queryWithClassName:@"_File"];
        [query whereKey:@"name" equalTo:[NSString stringWithFormat:@"%@.png",userId]];
        [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
            if (object != nil) {
                user.portraitUri = [object objectForKey:@"url"];
                
            }
            NSLog(@"%@,%@))))))))",user.name,user.portraitUri);
        }];
        
        
        return completion(user);
        
    }



@end
